package io.renren.modules.report.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.report.service.ReportService;
import io.renren.modules.work.entity.*;
import io.renren.modules.work.service.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ScConsumablesService scConsumablesService;
    @Autowired
    private ScProjectService scProjectService;
    @Autowired
    private ScProjectdtlService scProjectdtlService;
    @Autowired
    private ScGoodsService scGoodsService;
    @Autowired
    private ScReportService scReportService;
    @Autowired
    private ScQuotationService scQuotationService;
    @Autowired
    private ScWorkerService scWorkerService;
    @Autowired
    private ScSignService scSignService;
    @Autowired
    private ScFileService scFileService;
    @Autowired
    private ScExtrasService scExtrasService;

    @Value("${ImgServerIP}")
    private String imgServerIP;
    @Value("${ImgServerPort}")
    private String imgServerPort;
    @Value("${ImgUrl}")
    private String imgUrl;




    private static StringBuffer transformation(int number) {
        //定义大写Map
        Map<Integer,String> map = new HashMap<>();
        map.put(0,"零");
        map.put(1,"壹");
        map.put(2,"贰");
        map.put(3,"叄");
        map.put(4,"肆");
        map.put(5,"伍");
        map.put(6,"陆");
        map.put(7,"柒");
        map.put(8,"捌");
        map.put(9,"玖");
        ArrayList<String> list = new ArrayList<>();
        char[] chars = String.valueOf(number).toCharArray();
        int whichNumber = 0;
        for (int i = 0; i < chars.length; i++) {

                String value = map.get(Integer.valueOf(String.valueOf(chars[i])));
                list.add(value);
                //当执行了到这里时就知道小数点在多少位了
                whichNumber = i+1;

        }
        return splicing(list, whichNumber);
    }

    private static StringBuffer splicing(ArrayList<String> list,int whichNumber) {
        //定义每位数对应的范围
        Map<Integer, String> mapValue= new HashMap<>();
        mapValue.put(1,"元");
        mapValue.put(2,"十");
        mapValue.put(3,"百");
        mapValue.put(4,"千");
        mapValue.put(5,"万");
        mapValue.put(6,"十");
        mapValue.put(7,"百");
        mapValue.put(8,"千");
        mapValue.put(9,"亿");
        mapValue.put(10,"十");
        mapValue.put(0,"角");
        mapValue.put(-1,"分");
        mapValue.put(-2,"哩");
        StringBuffer stringBuffer = new StringBuffer();
        for (String s : list) {
            //定义小数点的值,每减一次,map的key也会变小
            if (whichNumber < 0) {
                stringBuffer = stringBuffer.append(s).append(mapValue.get(whichNumber));
            }else{
                stringBuffer.append(s).append(mapValue.get(whichNumber));
            }
            whichNumber --;
        }
        return stringBuffer;
    }





    @Override
    public void optPdf(HttpServletResponse response, String businessUuid, Integer optType) throws Exception {
        String fileName = "report1.jrxml";
        String pdfName = "test";
        //File file = ResourceUtils.getFile("classpath:ireport/" + fileName);
        //jar部署方式需要以以下方式获取文件
        InputStream is = ReportServiceImpl.class.getResourceAsStream("/ireport/" + fileName);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/pdf");
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(pdfName, "UTF-8") + ".pdf");

        try {
            //编译jasperReport
            JasperReport jasperReport = JasperCompileManager.compileReport(is);
            //添加iReport入参
            HashMap<String, Object> params = new HashMap<>();
            params.put("test",businessUuid);
            //获取outputStream
            OutputStream outputStream = response.getOutputStream();
            if(optType == 1){
                //预览
                byte[] bytes = JasperRunManager.runReportToPdf(jasperReport,params);
                response.setContentType("application/pdf;charset=UTF-8");
                response.setHeader("content-disposition","inline");
                response.setContentLength(bytes.length);
                outputStream.write(bytes, 0, bytes.length);
                outputStream.flush();
                outputStream.close();
            }else{
                //下载
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
                JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
            }

        } catch (IOException | JRException e) {
            e.printStackTrace();
        }
    }

    private int caculateNeedLength(int dataLength,int detailNum){
        if(dataLength<detailNum){
            return detailNum - dataLength;
        }else {
            return dataLength % detailNum;
        }
    }

    @Override
    public JasperPrint goodsConsumeReport(String id, String reportName) throws Exception {

        QueryWrapper<ScReportEntity> scReportEntityQueryWrapper = new QueryWrapper<>();
        scReportEntityQueryWrapper.eq("reportName",reportName);
        ScReportEntity reportEntity = scReportService.getOne(scReportEntityQueryWrapper);
        String filePath = "classpath:ireport/"+reportName;
        File file = ResourceUtils.getFile(filePath);
        JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());
        //细单数据
        List<Map<String, String>> data = new ArrayList<>();
        QueryWrapper<ScConsumablesEntity> scConsumablesEntityQueryWrapper = new QueryWrapper<>();
        scConsumablesEntityQueryWrapper.eq("projectid",id);
        List<ScConsumablesEntity> list = scConsumablesService.list(scConsumablesEntityQueryWrapper);
        int detailNum =reportEntity.getDetailnum();
        int dataLength = list.size();
        int needAddLength = caculateNeedLength(dataLength, detailNum);
        if(list.size()==0){
            throw new RuntimeException("没有数据");
        }
            if(dataLength<detailNum){
                needAddLength = detailNum - dataLength;
            }else {
                needAddLength = dataLength % detailNum;
            }

        for(int i = 0;i<list.size();i++){
            ScConsumablesEntity scConsumablesEntity = list.get(i);
            ScProjectdtlEntity scProjectdtlEntity = scProjectdtlService.getById(scConsumablesEntity.getProjectdtlid());
            ScGoodsEntity goods = scGoodsService.getById(scConsumablesEntity.getGoodsid());
            Map<String, String> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("goodsname",goods.getGoodsname());
            m.put("goodstype",goods.getGoodstype());
            m.put("goodsunit",goods.getGoodsunit());
            m.put("singleprice",scConsumablesEntity.getSingleprice().toString());
            m.put("price",scConsumablesEntity.getPrice().toString());
            m.put("source",scConsumablesEntity.getResource());
            m.put("num",scConsumablesEntity.getNum().toString());
            data.add(m);

        }
        if(needAddLength>0){
            for(int i= 0;i<needAddLength;i++){
                Map<String, String> m = new HashMap<>();
                data.add(m);
            }
        }
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);
        //主单数据
        ScProjectEntity scProjectEntity = scProjectService.getById(id);
        HashMap<String, Object> params = new HashMap<>();
        params.put("shipsideno",scProjectEntity.getShipsideno());
        params.put("projectno",scProjectEntity.getProjectno());
        params.put("cretime",scProjectEntity.getCretime());
        return JasperFillManager.fillReport(jasperReport, params, dataSource);
    }

    @Override
    public JasperPrint rgchkReport(String id, String reportName) throws Exception {
        QueryWrapper<ScReportEntity> scReportEntityQueryWrapper = new QueryWrapper<>();
        scReportEntityQueryWrapper.eq("reportName",reportName);
        ScReportEntity reportEntity = scReportService.getOne(scReportEntityQueryWrapper);
        String filePath = "classpath:ireport/"+reportName;
        File file = ResourceUtils.getFile(filePath);
        JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());
        //细单数据
        List<Map<String, String>> data = new ArrayList<>();
        QueryWrapper<ScProjectdtlEntity> projectdtlEntityQueryWrapper = new QueryWrapper<>();
        projectdtlEntityQueryWrapper.eq("projectid",id);
        List<ScProjectdtlEntity> list = scProjectdtlService.list(projectdtlEntityQueryWrapper);
        int detailNum =reportEntity.getDetailnum();
        int dataLength = list.size();
        int needAddLength = caculateNeedLength(dataLength, detailNum);
        if(list.size()==0){
            throw new RuntimeException("没有数据");
        }
        if(dataLength<detailNum){
            needAddLength = detailNum - dataLength;
        }else {
            needAddLength = dataLength % detailNum;
        }

        for(int i = 0;i<list.size();i++){
            ScProjectdtlEntity scProjectdtlEntity = list.get(i);
            Map<String, String> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("handleState",scProjectdtlEntity.getHandleState());
            m.put("qualityState",scProjectdtlEntity.getQualityState());
            m.put("memo",scProjectdtlEntity.getMemo());
            data.add(m);
        }
        if(needAddLength>0){
            for(int i= 0;i<needAddLength;i++){
                Map<String, String> m = new HashMap<>();
                data.add(m);
            }
        }
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);
        //主单数据
        ScProjectEntity scProjectEntity = scProjectService.getById(id);
        HashMap<String, Object> params = new HashMap<>();
        params.put("shipsideno",scProjectEntity.getShipsideno());
        params.put("projectno",scProjectEntity.getProjectno());
        params.put("cretime",scProjectEntity.getCretime());
        params.put("startTime",scProjectEntity.getStartTime());
        params.put("endTime",scProjectEntity.getEndTime());
        params.put("equipName",scProjectEntity.getEquipName());
        return JasperFillManager.fillReport(jasperReport, params, dataSource);
    }

    @Override
    public JasperPrint priceReviewReport(String id, String reportName) throws Exception{
        QueryWrapper<ScReportEntity> scReportEntityQueryWrapper = new QueryWrapper<>();
        scReportEntityQueryWrapper.eq("reportName",reportName);
        ScReportEntity reportEntity = scReportService.getOne(scReportEntityQueryWrapper);
        String filePath = "classpath:ireport/"+reportName;
        File file = ResourceUtils.getFile(filePath);
        JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());
        //细单数据
        List<Map<String,Object>> data = new ArrayList<>();
        QueryWrapper<ScProjectdtlEntity> projectdtlEntityQueryWrapper = new QueryWrapper<>();
        projectdtlEntityQueryWrapper.eq("projectid",id);
        List<ScProjectdtlEntity> list = scProjectdtlService.list(projectdtlEntityQueryWrapper);
        int detailNum =reportEntity.getDetailnum();
        int dataLength = list.size();
        int needAddLength = caculateNeedLength(dataLength, detailNum);
        if(list.size()==0){
            throw new RuntimeException("没有数据");
        }
        if(dataLength<detailNum){
            needAddLength = detailNum - dataLength;
        }else {
            needAddLength = dataLength % detailNum;
        }

        for(int i = 0;i<list.size();i++){
            ScProjectdtlEntity scProjectdtlEntity = list.get(i);
            Map<String, Object> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("price",scProjectdtlEntity.getPrice().doubleValue());
            m.put("workhours",scProjectdtlEntity.getWorkhours().doubleValue());
            m.put("manhourfee",scProjectdtlEntity.getManhourfee().doubleValue());
            m.put("specialfee",scProjectdtlEntity.getSpecialfee().doubleValue());
            m.put("materialcost",scProjectdtlEntity.getMaterialcost().doubleValue());
            m.put("memo",scProjectdtlEntity.getMemo());
            data.add(m);
        }
        if(needAddLength>0){
            for(int i= 0;i<needAddLength;i++){
                Map<String, Object> m = new HashMap<>();
                data.add(m);
            }
        }
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);
        //主单数据
        ScProjectEntity scProjectEntity = scProjectService.getById(id);
        HashMap<String, Object> params = new HashMap<>();
        params.put("shipsideno",scProjectEntity.getShipsideno());
        params.put("projectno",scProjectEntity.getProjectno());
        params.put("cretime",scProjectEntity.getCretime());
        params.put("equipName",scProjectEntity.getEquipName());
        params.put("totalPrice",scProjectEntity.getTotalprice().doubleValue());
        params.put("totalpriceCN",transformation(scProjectEntity.getTotalprice().intValue()).toString());

        return JasperFillManager.fillReport(jasperReport, params, dataSource);
    }

    @Override
    public JasperPrint quotationReport(String id, String reportName) throws Exception {
        QueryWrapper<ScReportEntity> scReportEntityQueryWrapper = new QueryWrapper<>();
        scReportEntityQueryWrapper.eq("reportName",reportName);
        ScReportEntity reportEntity = scReportService.getOne(scReportEntityQueryWrapper);
        String filePath = "classpath:ireport/"+reportName;
        File file = ResourceUtils.getFile(filePath);
        JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());

        //主单数据
        ScProjectdtlEntity scProjectdtlEntity = scProjectdtlService.getById(id);
        ScProjectEntity scProjectEntity = scProjectService.getById(scProjectdtlEntity.getProjectid());
        HashMap<String, Object> params = new HashMap<>();
        params.put("shipsideno",scProjectEntity.getShipsideno());
        params.put("projectno",scProjectEntity.getProjectno());
        params.put("cretime",scProjectEntity.getCretime());

        //细单数据
        List<Map<String, Object>> data = new ArrayList<>();
        QueryWrapper<ScQuotationEntity> scQuotationEntityQueryWrapper = new QueryWrapper<>();
        scQuotationEntityQueryWrapper.eq("projectdtlid",id);
        List<ScQuotationEntity> list = scQuotationService.list(scQuotationEntityQueryWrapper);
        int detailNum =reportEntity.getDetailnum();
        int dataLength = list.size();
        int needAddLength = caculateNeedLength(dataLength, detailNum);
        if(list.size()==0){
            throw new RuntimeException("没有数据");
        }
        if(dataLength<detailNum){
            needAddLength = detailNum - dataLength;
        }else {
            needAddLength = dataLength % detailNum;
        }

        for(int i = 0;i<list.size();i++){
            ScQuotationEntity scQuotationEntity = list.get(i);
            Map<String, Object> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("factoryname",scQuotationEntity.getFactoryname());
            m.put("period",scQuotationEntity.getPeriod().toString());
            m.put("price",scQuotationEntity.getPrice().doubleValue());
            m.put("memo",scQuotationEntity.getMemo());
            data.add(m);
        }
        if(needAddLength>0){
            for(int i= 0;i<needAddLength;i++){
                Map<String, Object> m = new HashMap<>();
                data.add(m);
            }
        }
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);

        return JasperFillManager.fillReport(jasperReport, params, dataSource);
    }

    @Override
    public JasperPrint workerReport(String id, String reportName) throws Exception {
        QueryWrapper<ScReportEntity> scReportEntityQueryWrapper = new QueryWrapper<>();
        scReportEntityQueryWrapper.eq("reportName",reportName);
        ScReportEntity reportEntity = scReportService.getOne(scReportEntityQueryWrapper);
        String filePath = "classpath:ireport/"+reportName;
        File file = ResourceUtils.getFile(filePath);
        JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());

        //主单数据
        ScProjectdtlEntity scProjectdtlEntity = scProjectdtlService.getById(id);
        ScProjectEntity scProjectEntity = scProjectService.getById(scProjectdtlEntity.getProjectid());
        HashMap<String, Object> params = new HashMap<>();
        params.put("shipsideno",scProjectEntity.getShipsideno());
        params.put("projectno",scProjectEntity.getProjectno());
        params.put("cretime",scProjectEntity.getCretime());

        //细单数据
        List<Map<String, Object>> data = new ArrayList<>();
        QueryWrapper<ScSignEntity> scSignEntityQueryWrapper = new QueryWrapper<>();
        scSignEntityQueryWrapper.eq("projectdtlid",id);
        List<ScSignEntity> list = scSignService.list(scSignEntityQueryWrapper);
        int detailNum =reportEntity.getDetailnum();
        int dataLength = list.size();
        int needAddLength = caculateNeedLength(dataLength, detailNum);
        if(list.size()==0){
            throw new RuntimeException("没有数据");
        }
        if(dataLength<detailNum){
            needAddLength = detailNum - dataLength;
        }else {
            needAddLength = dataLength % detailNum;
        }

        for(int i = 0;i<list.size();i++){
            ScSignEntity scSignEntity = list.get(i);
            ScWorkerEntity workerEntity = scWorkerService.getById(scSignEntity.getWorkerid());
            Map<String, Object> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("repairCompany","三成科技有限公司");
            m.put("workername",workerEntity.getWorkername());
            m.put("idcard",workerEntity.getIdcard());
            m.put("workertype",workerEntity.getWorkertype());
            m.put("signtime",scSignEntity.getSigntime());
            m.put("leavetime",scSignEntity.getLeavetime());
            m.put("memo",scSignEntity.getMemo());
            data.add(m);
        }
        if(needAddLength>0){
            for(int i= 0;i<needAddLength;i++){
                Map<String, Object> m = new HashMap<>();
                data.add(m);
            }
        }
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);

        return JasperFillManager.fillReport(jasperReport, params, dataSource);
    }

    @Override
    public JasperPrint askReport(String id, String reportName) throws Exception {
        QueryWrapper<ScReportEntity> scReportEntityQueryWrapper = new QueryWrapper<>();
        scReportEntityQueryWrapper.eq("reportName",reportName);
        ScReportEntity reportEntity = scReportService.getOne(scReportEntityQueryWrapper);
        String filePath = "classpath:ireport/"+reportName;
        File file = ResourceUtils.getFile(filePath);
        JasperReport jasperReport = JasperCompileManager.compileReport(file.getPath());
        //细单数据
        List<Map<String,Object>> data = new ArrayList<>();
        QueryWrapper<ScProjectdtlEntity> projectdtlEntityQueryWrapper = new QueryWrapper<>();
        projectdtlEntityQueryWrapper.eq("projectid",id);
        List<ScProjectdtlEntity> list = scProjectdtlService.list(projectdtlEntityQueryWrapper);
        int detailNum =reportEntity.getDetailnum();
        int dataLength = list.size();
        int needAddLength = caculateNeedLength(dataLength, detailNum);
        if(list.size()==0){
            throw new RuntimeException("没有数据");
        }
        if(dataLength<detailNum){
            needAddLength = detailNum - dataLength;
        }else {
            needAddLength = dataLength % detailNum;
        }

        for(int i = 0;i<list.size();i++){
            ScProjectdtlEntity scProjectdtlEntity = list.get(i);
            Map<String, Object> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("skill",scProjectdtlEntity.getSkill());
            m.put("brokenstatus",scProjectdtlEntity.getProjectdtlname()+scProjectdtlEntity.getBrokenstatus());
            m.put("memo",scProjectdtlEntity.getMemo());
            data.add(m);
        }
        if(needAddLength>0){
            for(int i= 0;i<needAddLength;i++){
                Map<String, Object> m = new HashMap<>();
                data.add(m);
            }
        }
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);
        //主单数据
        ScProjectEntity scProjectEntity = scProjectService.getById(id);
        HashMap<String, Object> params = new HashMap<>();
        params.put("shipsideno",scProjectEntity.getShipsideno());
        params.put("projectno",scProjectEntity.getProjectno());
        params.put("cretime",scProjectEntity.getCretime());
        params.put("equipName",scProjectEntity.getEquipName());
        return JasperFillManager.fillReport(jasperReport, params, dataSource);
    }

    @Override
    public JasperPrint picReport(String id, String reportName) throws Exception {
//        String filePath = "classpath:ireport/PicReport.jrxml";
//        File file = ResourceUtils.getFile(filePath);
        ClassPathResource template = new ClassPathResource("ireport"+File.separator+"PicReport.jrxml");
        InputStream templateInputStream = template.getInputStream();
        JasperReport jasperReport = JasperCompileManager.compileReport(templateInputStream);
        //细单数据
        List<Map<String,Object>> data = new ArrayList<>();
        QueryWrapper<ScFileEntity> scFileEntityQueryWrapper = new QueryWrapper<>();
        scFileEntityQueryWrapper.eq("projectdtlid",id);
        List<ScFileEntity> list = scFileService.list(scFileEntityQueryWrapper);
        Map<String, Object> m = new HashMap<>();
        for(int i =0;i<list.size();i++){
            ScFileEntity scFileEntity = list.get(i);
            m.put("img"+(i+1),scFileEntity.getFilename());
        }
        data.add(m);
        JRDataSource dataSource = new JRBeanCollectionDataSource(data);
        //主单数据
        HashMap<String, Object> params = new HashMap<>();
        params.put("BaseUrl",imgUrl);
        params.put("port","");
        return JasperFillManager.fillReport(jasperReport, params, dataSource);
    }



    @Override
    public void goodsConsumeExcel(String id, String reportName, ExcelWriter excelWriter) throws Exception {
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        //主单数据
        ScProjectEntity scProjectEntity = scProjectService.getById(id);
        HashMap<String, Object> header = new HashMap<>();
        header.put("shipsideno",scProjectEntity.getShipsideno());
        header.put("projectno",scProjectEntity.getProjectno());
        header.put("cretime",scProjectEntity.getCretime());


        //细单数据
        List<Map<String, String>> data = new ArrayList<>();
        QueryWrapper<ScConsumablesEntity> scConsumablesEntityQueryWrapper = new QueryWrapper<>();
        scConsumablesEntityQueryWrapper.eq("projectid",id);
        List<ScConsumablesEntity> list = scConsumablesService.list(scConsumablesEntityQueryWrapper);
        for(int i = 0;i<list.size();i++){
            ScConsumablesEntity scConsumablesEntity = list.get(i);
            ScProjectdtlEntity scProjectdtlEntity = scProjectdtlService.getById(scConsumablesEntity.getProjectdtlid());
            ScGoodsEntity goods = scGoodsService.getById(scConsumablesEntity.getGoodsid());
            Map<String, String> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("goodsname",goods.getGoodsname());
            m.put("goodstype",goods.getGoodstype());
            m.put("goodsunit",goods.getGoodsunit());
            m.put("singleprice",scConsumablesEntity.getSingleprice().toString());
            m.put("price",scConsumablesEntity.getPrice().toString());
            m.put("source",scConsumablesEntity.getResource());
            m.put("num",scConsumablesEntity.getNum().toString());
            data.add(m);
        }
        excelWriter.fill(header,fillConfig,writeSheet);
        excelWriter.fill(data,fillConfig,writeSheet);

    }

    @Override
    public void rgchkExcel(String id, String reportName, ExcelWriter excelWriter) throws Exception {
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        //主单数据
        ScProjectEntity scProjectEntity = scProjectService.getById(id);
        HashMap<String, Object> header = new HashMap<>();
        header.put("shipsideno",scProjectEntity.getShipsideno());
        header.put("projectno",scProjectEntity.getProjectno());
        header.put("cretime",scProjectEntity.getCretime());
        header.put("startTime",scProjectEntity.getStartTime());
        header.put("endTime",scProjectEntity.getEndTime());
        header.put("equipName",scProjectEntity.getEquipName());

        //细单数据
        List<Map<String, String>> data = new ArrayList<>();
        QueryWrapper<ScProjectdtlEntity> projectdtlEntityQueryWrapper = new QueryWrapper<>();
        projectdtlEntityQueryWrapper.eq("projectid",id);
        List<ScProjectdtlEntity> list = scProjectdtlService.list(projectdtlEntityQueryWrapper);
        for(int i = 0;i<list.size();i++){
            ScProjectdtlEntity scProjectdtlEntity = list.get(i);
            Map<String, String> m = new HashMap<>();
            Integer dNum = i+1;
            m.put("detailNum",dNum.toString());
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("handleState",scProjectdtlEntity.getHandleState());
            m.put("qualityState",scProjectdtlEntity.getQualityState());
            m.put("memo",scProjectdtlEntity.getMemo());
            data.add(m);
        }

        excelWriter.fill(header,fillConfig,writeSheet);
        excelWriter.fill(data,fillConfig,writeSheet);
    }

    @Override
    public void priceReview(String id, String reportName, ExcelWriter excelWriter) throws Exception {
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        int dNUm = 1;
        //主单数据
        ScProjectEntity scProjectEntity = scProjectService.getById(id);
        HashMap<String, Object> header = new HashMap<>();
        header.put("shipsideno",scProjectEntity.getShipsideno());
        header.put("projectno",scProjectEntity.getProjectno());
        header.put("cretime",scProjectEntity.getCretime());
        header.put("equipName",scProjectEntity.getEquipName());
        //总金额
        BigDecimal totalprice = scProjectEntity.getTotalprice();

        //细单数据
        List<Map<String,Object>> data = new ArrayList<>();
        QueryWrapper<ScProjectdtlEntity> projectdtlEntityQueryWrapper = new QueryWrapper<>();
        projectdtlEntityQueryWrapper.eq("projectid",id);
        List<ScProjectdtlEntity> list = scProjectdtlService.list(projectdtlEntityQueryWrapper);
//        double totalFee = 0;

        //子工程数据
        for(int i = 0;i<list.size();i++){
            ScProjectdtlEntity scProjectdtlEntity = list.get(i);
            Map<String, Object> m = new HashMap<>();

            m.put("detailNum",dNUm);
            m.put("projectdtlname",scProjectdtlEntity.getProjectdtlname());
            m.put("price",scProjectdtlEntity.getPrice()==null?"":scProjectdtlEntity.getPrice().doubleValue());
            m.put("workhours",scProjectdtlEntity.getWorkhours()==null?"":scProjectdtlEntity.getWorkhours().doubleValue());
            m.put("manhourfee",scProjectdtlEntity.getManhourfee()==null?"":scProjectdtlEntity.getManhourfee().doubleValue());
            m.put("specialfee",scProjectdtlEntity.getSpecialfee()==null?"":scProjectdtlEntity.getSpecialfee().doubleValue());
            m.put("materialcost",scProjectdtlEntity.getMaterialcost()==null?"":scProjectdtlEntity.getMaterialcost().doubleValue());
            m.put("memo",scProjectdtlEntity.getMemo());
            m.put("null","");
            data.add(m);
            dNUm++;
        }

        //杂费数据
        QueryWrapper<ScExtrasEntity> scExtrasEntityQueryWrapper = new QueryWrapper<>();
        scExtrasEntityQueryWrapper.eq("projectid",id);
        List<ScExtrasEntity> extrasList = scExtrasService.list(scExtrasEntityQueryWrapper);
        for (ScExtrasEntity scExtrasEntity : extrasList) {
            Map<String, Object> m = new HashMap<>();
            m.put("detailNum",dNUm);
            m.put("projectdtlname",scExtrasEntity.getExtrasName());
            m.put("price",scExtrasEntity.getPrice()==null?"":scExtrasEntity.getPrice().doubleValue());
            m.put("memo",scExtrasEntity.getMemo());
            m.put("workhours","");
            m.put("manhourfee","");
            m.put("specialfee","");
            m.put("materialcost","");
            m.put("null","");

            data.add(m);
            totalprice = totalprice.add(scExtrasEntity.getPrice()==null?new BigDecimal("0"):scExtrasEntity.getPrice());

        }

        //主单金额
        header.put("totalPrice",totalprice.doubleValue());
        header.put("totalpriceCN",transformation(totalprice.intValue()).toString());

        excelWriter.fill(header,fillConfig,writeSheet);
        excelWriter.fill(data,fillConfig,writeSheet);
    }
}
